layui.use(['table', 'laydate', 'layer', 'form', 'laytpl'], function() {
	var $ = layui.$;
	var table = layui.table;
	var layer = layui.layer;
	var laytpl = layui.laytpl;
	var laydate = layui.laydate;
	var postTime;
	roleList();

	function roleList() {
		$.ajax({
			type: "post",
			url: "/roleList",
			async: true,
			data: {
				postTime: postTime
			},
			success: function(data) {
				var result = $.parseJSON(data);
				var tablelist = table.render({
					elem: '#test',
					height: 'full-95',
					page: true, //开启分页,
					limits: [10],
					limit: 10,
					id: 'table_info',
					cols: [
						[ //表头
							{
								checkbox: true,
								fixed: true
							},
							{
								field: 'rolecode',
								title: '角色编码',
								width: 200,
							}, {
								field: 'rolename',
								title: '角色值',
								width: 200,
								sort: true,
							}, {
								field: 'rolediscript',
								title: '角色描述',
								width: 250,
							}, {
								field: 'rolelv',
								title: '角色等级',
								width: 158
							},
							{
								fixed: 'right',
								title: '操作',
								width: 300,
								align: 'center',
								toolbar: '#barDemo'
							}
						]
					],
					"data": result
				});
			}
		});
	}
	//分页
	laydate.render({
		elem: '#postTime' //指定元素
	});
	table.on('tool(lay_table_filter)', function(obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
		var data = obj.data //获得当前行数据,
		layEvent = obj.event; //获得 lay-event 对应的值
		if(layEvent === 'edit') {
			detail(data.id, "edit");
		} else if(layEvent === 'detail') {
			checkDetails(data.id, "detail");
		} else if(layEvent === 'del') {
			deleteMore(data.id)
		} else if(layEvent === 'role') {
			authorise(data.id);
		}
	});
	$("#addBtn").on("click", function() {
		detail(" ", "add");
	});
	//编辑客户信息
	function detail(id, type) {
		var iframeId = new Date().getTime().toString();
		layer.open({
			id: iframeId,
			title: '编辑角色信息',
			type: 2,
			content: '/static/msg/role/role_edit.html?id=' + id + "&type=" + type,
			area: ['360px', '340px'],
			anim: -1, //关闭动画 兼容ie6-9需设置此项
			isOutAnim: false, //关闭动画  兼容ie6-9需设置此项
			shadeClose: false,
			btn: ['确认', '取消'],
			yes: function(index, layero) {
				var body = layer.getChildFrame('body', index);
				var rolecode = body.find(".rolecode").val();
				var rolename = body.find(".rolename").val();
				var rolediscript = body.find(".rolediscript").val();
				var rolelv = body.find(".rolelv").val();
				var isgo = true;
				if(!rolecode || rolecode == "" || rolecode == undefined) {
					layer.msg('用户名不能为空', {
						time: 2000
					});
					isgo = false;
				}
				if(isgo) {
					if(!rolename || rolename == "" || rolename == undefined) {
						layer.msg('邮箱不能为空', {
							time: 2000
						});
						isgo = false;
					}
				}
				if(isgo) {
					if(!rolediscript || rolediscript == "" || rolediscript == undefined) {
						layer.msg('手机号不能为空', {
							time: 2000
						});
						isgo = false;
					}
				}
				if(isgo) {
					if(!rolelv || rolelv == "" || rolelv == undefined) {
						layer.msg('角色等级不能为空', {
							time: 2000
						});
						isgo = false;
					}
				}
				if(isgo) {
					tijiao();
				}

				function tijiao() {
					$.ajax({
						type: "post",
						url: "/role/" + type,
						data: {
							id: id,
							rolecode: rolecode,
							rolename: rolename,
							rolediscript: rolediscript,
							rolelv: rolelv
						},
						async: true,
						success: function(data) {
							layer.msg(data.massage, {
								time: 1000
							}, function() {
								if(data.code == '1') {
									layer.close(index);
									roleList();
								}
							});
						}
					});
				}
			}
		});
	}
	//查看角色信息
	function checkDetails(id, type) {
		var iframeId = new Date().getTime().toString();
		layer.open({
			id: iframeId,
			title: '角色详情',
			type: 2,
			content: '/static/msg/role/role_edit.html?id=' + id + "&type=" + type,
			area: ['360px', '340px'],
			anim: -1, //关闭动画 兼容ie6-9需设置此项
			isOutAnim: false, //关闭动画  兼容ie6-9需设置此项
			shadeClose: false,
			btn: ['确认', '取消'],
			yes: function(index, layero) {
				layer.close(index);
			}
		});
	}
	//表格中的授权事件
	function authorise(id) {
		var iframeId = new Date().getTime().toString();
		layer.open({
			id: iframeId,
			title: '角色详情',
			type: 2,
			content: '/static/msg/role/role_author.html?id=' + id,
			area: ['360px', '480px'],
			anim: -1, //关闭动画 兼容ie6-9需设置此项
			isOutAnim: false, //关闭动画  兼容ie6-9需设置此项
			shadeClose: false,
			btn: ['确认', '取消'],
			yes: function(index, layero) {
				layer.close(index);
			}
		});
	}
	//表格中的删除事件
	function deleteMore(id) {
		if("" == id) {
			layer.alert("请至少选择一条信息!");
			return;
		}
		layer.confirm('确认删除此信息吗？', {
			icon: 3,
			title: '提示'
		}, function(index) {
			$.ajax({
				type: "post",
				url: "/getuserrole",
				async: true,
				data: {
					id: id
				},
				success: function(data) {
					if(data != 'a') {
						layer.msg('该角色拥有用户，不能删除', {
							time: 2000
						});
					} else {
						delrole();
					}
				}
			});

			function delrole() {
				$.ajax({
					type: "post",
					url: "/role/delect",
					async: true,
					data: {
						id: id
					},
					success: function(data) {
						layer.msg(data.message);
						roleList();
					}
				});
			}
			layer.close(index);
		});
	}
});